Newsgroups: comp.lang.tcl,comp.answers,news.answers Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!spool.mu.edu!darwin.sura.net!europa.eng.gtefsd.com!news.ans.net!malgudi.oar.net!chemabs!lvirden From: lwv26@cas.org (Larry W. Virden) Subject: FAQ: comp.lang.tcl Frequently Asked Questions (2/5) (Last updated: August 10, 1993) Message-ID: Followup-To: comp.lang.tcl Summary: A regular posting of the comp.lang.tcl Frequently Asked Questions (FAQ) and their answers. This is the second of five parts. This part covers the how-to questions and answers. Originator: lwv26@lwv26aws Keywords: tcl, expect, extended tcl, wish, tk Sender: lvirden@cas.org Reply-To: lvirden@cas.org (Larry W. Virden) Organization: Chemical Abstracts Service References: Date: Wed, 18 Aug 1993 20:07:26 GMT Approved: news-answers-request@MIT.Edu Expires: Wed, 29 Sep 1993 20:07:01 GMT Lines: 1258 Xref: senator-bedfellow.mit.edu comp.lang.tcl:6246 comp.answers:1657 news.answers:11553 Archive-name: tcl-faq/part2 Version: 4.0 Last-modified: August 10, 1993 Please do not be offended if I neglect to acknowledge your contribution to this FAQ! I sometimes forget to put an attribution in. I especially try to add them if it appears that there is further experimentation or debugging being done though. For more information concerning Tcl (see "tcl-faq/part1"), (see "tcl-faq/part3"), (see "tcl-faq/part4"), or (see "tcl-faq/part5"). Index of questions: VIII. Questions on building Tcl and friends on your system: Q8A. Is there anywhere I can find help with the details of getting Tcl to work on my machine? Q8B. Why does the link step says that some of the functions Tcl, needs are missing when I am trying to build it? Q8C. Has anyone built Tcl 6 on an RS/6000 AIX 3.1? Q8D. Has anyone gotten Tcl to compile under HP-UX? Q8E. Has anyone gotten Tcl to compile under VMS? Q8F. What does it take to get Tcl to compile under SCO Unix? Q8G. Why do I get format and scan errors when I run tclTest (NeXT, AIX, etc.)? Q8H. Why do I get lots of errors under Irix 4.0.1 when I run tclTest? Q8I. Does anyone else have problems with Tcl on a Cray? Q8J. Does anyone know how to get Tk to run on a SparcBook or other laptop with a limited number of colors? Q8K. What does it take to get Tcl/Tk to compile on 386bsd/Linux or other POSIX/ANSI C systems not already supported? Q8L. Can anyone help me build tcl 6.7 under NextStep 3.0? Q8M. Why do I get a parse error in tkInit.c when I try to compile Tk on my Irix machine? IX. How, in Tcl, can I XXX: Q9A. get association lists or property lists? Q9B. call one proc with the multi parameter value returned by another proc? Q9C. pass an array into a proc? Q9D. pipe output of a command back into a Tcl parsing procedure? Q9E. merge extended Tcl into other programs such as wish or expect? Q9F. delete a procedure from within a script? Q9G. get parray to recognize an array variable I have created via upvar? Q9H. get more than 7 digits of double precision? Q9I. grab the command line whenever a non-built-in call is made? Q9J. get or set an environment variable? Q9K. use numbers with leading zeroes? Q9L. find the command line arguments to my application? Q9M. put comments in my script, for example in a case statement? Q9N. redirect stdin or stdout safely, including binary data? Q9O. trap signals, and other more Unix specific functions? Q9P. get quoted strings to work the way I want? Q9Q. share procedures between multiple tcl applications? X. How, in Tk, can I XXX: A. Question on starting Tk applications Q10.A.1. get my wish application to execute - I just get a wish prompt! Q10.A.2. ,using a machine with less than 8 bit color, run? Q10.A.3. set X11 resources for a wish application in an app-defaults file? Q10.A.4. specify bitmap patterns on the command line instead of as a file name? Q10.A.5. get a Motif user interface? Q10.A.6. get an OpenLook user interface? B. Questions on Tk applications and the keyboard Q10.B.1. change the default class bindings? Q10.B.2. delete a binding? Q10.B.3. change a binding while it is being executed? Q10.B.4. bind the arrow key on my Sun keyboard? Q10.B.5. get root's mouse bindings to work in my Tk application? C. Questions on Tk and X11 interactions Q10.C.1. get an application to also use libXt? Q10.C.2. change the X11 cursor? Q10.C.3. raise or lower a window? Q10.C.4. re-map a withdrawn window id? Q10.C.5. use Tk in a subwindow of a non-Tk X11 application? D. Questions on Tk listboxes Q10.D.1. resize a listbox? Q10.D.2. select two items that are not adjacent in the listbox at one time? Q10.D.3. select items in more than one Tk listbox at a time? E. Questions on Tk canvases Q10.E.1. get output from a Tk canvas? Q10.E.2. fill a canvas which is bounded by lines as opposed to a shape like a polygon, oval, etc.? Q10.E.3. raise/lower canvas window objects or draw graphics onto a window object inside a canvas? F. Other questions Q10.F.1. get the name of my own interpreter? Q10.F.2. get -relief to work on my text widgets? Q10.F.3. create a scrollable window of buttons? Q10.F.4. pack a text widget so that it can be resized interactively? Q10.F.5. create a widget with an upper case name? Q10.F.6. create equal sized buttons? End of FAQ Index ---------------------------------------------------------------------- ------------------------------ From: -VIII- Questions on building Tcl and friends on your system Subject: -Q8A- Is there anywhere I can find help with the details of getting Tcl to work on my machine? A8A. Glad you asked! Look in the Tcl distribution for the file called "porting.notes". This will contain a collection of notes that various people have provided about porting Tcl to various machines and operating systems. There are also a file called "README" which should be read FIRST - before doing anything else with the code (this should always be one's first step with any package). Finally, there is a "changes" file which details what has changed since the last release - be sure to read this to see what might need to change in your programs. ------------------------------ From: -VIII- Questions on building Tcl and friends on your system Subject: -Q8B- Why does the link step says that some of the functions Tcl, needs are missing when I am trying to build it? A8B. Did you run the "config" program first, by doing a "csh ./config" or equivalent? Without doing this, things such as strtoul or strerror are sometimes mentioned as missing. Tcl includes equivalents for at least the following functions and include files which may not be found on some systems: dirent.h limits.h stdlib.h string.h opendir.c strerror.c strstr.c strtol.c strtoul.c strtod.c ------------------------------ From: -VIII- Questions on building Tcl and friends on your system Subject: -Q8C- Has anyone built Tcl 6 on an RS/6000 AIX 3.1? A8C. See porting notes - especially the note about strtoul. One user got Tcl to compile with a few minor source modifications (for example, duplicate case statements for errno and signal symbols in tclUnixStr.c). A few other problems arose in the tests. One is caused by AIX printf not formatting %#x and %#o correctly when the value to be printed is zero: they print "0x0" and "00" instead of "0" and "0" respectively. This was reported as not a problem in earlier releases. No fixes have been posted. Finally, a problem occurs in open.test. test 13.6 hangs because "cat" on the RS6000 is unbuffered. A workaround is to change the execution of "cat" in open.test to do a "cat -u". Dov Grobgeld provided info on creating Tcl and Tk shared libraries under AIX 3.1.5: For Tcl: cc -o tkshar.o *.o -bE:tclshar.exp -bM:SRE -berok -lX11 -lm ar r libtclshr tclshar.o For Tk: cc -o tkshar.o *.o -bE:tkshar.exp -bM:SRE -berok -Ltcl -lX11 -lm -ltclshr ar r libtkshr tkshar.o where tckshar.exp and tkshar.exp had lists of the external functions. There are also problems with fonts on AIX and the IBM. A patch is needed from IBM to fix the X server so that fonts are working ------------------------------ From: -VIII- Questions on building Tcl and friends on your system Subject: -Q8D- Has anyone gotten Tcl to compile under HP-UX? A8D. See the porting notes. ------------------------------ From: -VIII- Questions on building Tcl and friends on your system Subject: -Q8E- Has anyone gotten Tcl to compile under VMS? A8E. Information from jkimball@src.honeywell.com (John Kimball) on May 4, 1993 was that he had gotten Tcl 6.7 and Tk 3.2 ported to VMS 5.5. See the catalog for the file information. ------------------------------ From: -VIII- Questions on building Tcl and friends on your system Subject: -Q8F- What does it take to get Tcl to compile under SCO Unix? A8F. Add a "#undef select" to tkEvent.c, and remove the reference to TK_EXCEPTION around line 460 of main.c. Tk uses its own scheme for allocating the border colors for its 3D widgets, which causes problems when running TK on a system with "PseudoColor" display class, and a 16-cell colormap. If you can't go to eight bitplanes, you can instead start the server with a "-static" (Xsco) or "-analog" (Xsight) option, making the display class become "StaticColor". This makes the entire colormap read-only, and it will return the color that most closely maps to the desired color as possible. This information is from Keith Amann ------------------------------ From: -VIII- Questions on building Tcl and friends on your system Subject: -Q8G- Why do I get format and scan errors when I run tclTest (NeXT, AIX, etc.)? A8G. That's a problem (scanf/printf) many systems seem to have. Don't worry too much about it - just don't use these 'advanced' features. If you're hacking C, you'll have the same problems. ------------------------------ From: -VIII- Questions on building Tcl and friends on your system Subject: -Q8H- Why do I get lots of errors under Irix 4.0.1 when I run tclTest? A8H. There's a bug in the 4.0.1 optimizer that's fixed in 4.0.2. Compile tclVar.c using -O0 (no optimization). ------------------------------ From: -VIII- Questions on building Tcl and friends on your system Subject: -Q8I- Does anyone else have problems with Tcl on a Cray? A8I. See the porting notes for a set of changes mentioned. Also, Booker C. Bense reports that version 3.0.1.6 has some real problems with char pointers, causing Tcl to crash. Using version 3.0.2.1, things are much better, except for a minor formatting problem and serious problems with scan. ------------------------------ From: -VIII- Questions on building Tcl and friends on your system Subject: -Q8J- Does anyone know how to get Tk to run on a SparcBook or other laptop with a limited number of colors? A8J. On a SparcBook, if you start openwin (the OpenWindows server starting command) as: openwin -dev "/dev/fb staticvis" you get a static visual color model that Tk copes with better than the default. Some things are ugly, but not as ugly as monochrome. ------------------------------ From: -VIII- Questions on building Tcl and friends on your system Subject: -Q8K- What does it take to get Tcl/Tk to compile on 386bsd/Linux or other Posix/ANSI C systems not already supported? A8K. Patches for 386BSD were posted to comp.lang.tcl back in Nov. 1992 to alt.sources. See one of the ftp archive sites for this group for them. Basically, there were some setting of defines and a few places where const char * had to be used in place of char *. ------------------------------ Subject: -Q8L- Can anyone help me build tcl 6.7 under NextStep 3.0? A8L. Put #include near the top of tclUnixAZ.c. Thanks to Michael B. Johnson . ------------------------------ Subject: -Q8M- Why do I get a parse error in tkInit.c when I try to compile Tk on my Irix machine? A8M. Jon Knight reports that the version of Irix that he is using doesn't define a uid_t. One needs to define one somewhere (or comment out the lines causing the problems) to get the compile to continue. ------------------------------ From: -IX- How, in Tcl, can I XXX: Subject: -Q9A- association lists or property lists? A9A. Use Extended Tcl arrays or keyed lists. For example, if you did a: keylset ttyFields ttyName tty1a keylset ttyFields baudRate 57600 keylset ttyFields parity strip And then an "echo $ttyFields", you'd get: {ttyName tty1a} {baudRate 57600} {parity strip} ------------------------------ From: -IX- How, in Tcl, can I XXX: Subject: -Q9B- call one proc with the multi parameter value returned by another proc? A9B. Assuming y requires multiple args and x returns multiple words, use Tcl's eval command : eval y [x] ------------------------------ From: -IX- How, in Tcl, can I XXX: Subject: -Q9C- pass an array into a proc? A9C. Use upvar rather than try to use global variables when possible. If the function is event driven, you are forced to use global variables. # print elements of an array proc show_array arrayName { upvar $arrayName myArray foreach element [array names myArray] { puts stdout "${arrayName}($element) = $myArray($element)" } } set arval(0) zero set arval(1) one show_array arval To return an array from a procedures, just take the array name in as an argument, as above. Any changes you make in the array will be made in the parent's array as well. Extended Tcl introduces a concept called keyed lists which are arrays made out of lists of key-value pairs and can be passed by value to routines, over networks, etc. ------------------------------ From: -IX- How, in Tcl, can I XXX: Subject: -Q9D- pipe output of a command back into a Tcl parsing procedure? A9D. For example, to grep a pattern out of a range of files, one might do: karl@NeoSoft.com (Karl Lehenbauer) writes: set files [glob /home/cole/stats/*] proc parseInfo { site } { global files # # site is chosen from a listbox earlier # set in [open [concat "|/usr/bin/grep $site $files"] r] while {[gets $in line]>-1} { puts stderr $line } catch {close $in} } One thing: the matching strings are _not_ returned in directory order. But what if I want to check the return code AND use the output of the command? kennykb@dssv01.crd.ge.com (Kevin B. Kenny) writes: if [catch {exec ls} data] { # The exec got an error, and $errorCode has its termination status } else { # The exec succeeded } # In any case, `data' contains all the output from the child process. Note that Karl Lehenbauer adds that errorCode will be a list containing three elements, the string "CHILDSTATUS", the process ID of the child, and the exit status of the child. ------------------------------ From: -IX- How, in Tcl, can I XXX: Subject: -Q9E- merge extended Tcl into other programs such as wish or expect? A9E. The latest version of extended Tcl, tclX 6.7c, has been enhanced to make it easier to incorporate into applications. ------------------------------ From: -IX- How, in Tcl, can I XXX: Subject: -Q9F- delete a procedure from within a script? A9F. rename procedureName "" ------------------------------ From: -IX- How, in Tcl, can I XXX: Subject: -Q9G- get parray to recognize an array variable I have created via upvar? A9G. Right now (June, 1992) upvar doesn't allow you to attach to an individual element of an array. This is considered a bug by Mr. Ousterhout and has been place on a bug list. ------------------------------ From: -IX- How, in Tcl, can I XXX: Subject: -Q9H- get more than 7 digits of double precision ? A9H. Modify the tclExpr.c module to use %lf instead of %g. ------------------------------ From: -IX- How, in Tcl, can I XXX: Subject: -Q9I- grab the command line whenever a non-built-in call is made? A9I. The procedure "unknown" is called automatically with arguments containing the command and its arguments for any command that couldn't be found. In fact, Tcl and Extended Tcl use this feature to provide demand loaded commands, and even entire libraries. So by modifying the unknown procedure you can provide your own extended functionality, or even remove the demand loading capability if you so desire. ------------------------------ From: -IX- How, in Tcl, can I XXX: Subject: -Q9J- get or set an environment variable? A9J. By using something like the following. set olddisplay $env(DISPLAY) set env(DISPLAY) unix:0 Thanks to "Joel Fine" for the answer. ------------------------------ From: -IX- How, in Tcl, can I XXX: Subject: -Q9K- use numbers with leading zeroes? A9K. Dave Morriss was recently having problems because he was trying to do something like: set index [expr [exec date +%W]%[llength $pop_server_list]] but during the 9th and 9th week of the year, he got errors - 08 and 09 are not valid octal numbers in Tcl. Some of the solutions provided were: From George A. Howlett , we got: set wknum [format "%g" [exec date +%W]] set index [expr [exec $wknum%[llength $pop_server_list]] From Fred Feirtag : set index [expr (1[exec date +%W]-100)%[llength $pop_server_list]] From Dan R. Schenck : set index [expr [string trimleft [exec date +%W] 0]%[llength $pop_server_list]] ------------------------------ From: -IX- How, in Tcl, can I XXX: Subject: -Q9L- find the command line arguments to my application? A9L. If you are using extended Tcl or Expect, you will find the parameters in the Tcl variable argv as a list. Note that in extended Tcl, the name of the program is in the Tcl variable programName and NOT in argv[0]. Thanks to brad@NeoSoft.com (Brad Morrison) and bachww@rtsg.mot.com (Bud Bach) for this answer. ------------------------------ From: -IX- How, in Tcl, can I XXX: Subject: -Q9M- put comments in my script, for example in a case statement? A9M. You can't have comments where you have them. Move the comments inside of the "{" for the case that you want. Your code should read: case 1 { -1 { # # Cannot find information sought # exit 2 } 0 { # # Error in arguments # exit 1 } default { # # Desired information found # exit 0 } } Thanks to gwlester@cpu.com (Gerald W. Lester). ------------------------------ From: -IX- How, in Tcl, can I XXX: Subject: -Q9N- redirect stdin or stdout safely, including binary data? A9N. With Extended Tcl you can safely do stuff like: set infp [open "|compress -dc $fileName"] set outfp [open "|gzip -c $newFileName" w] copyfile $infp $outfp Thanks to karl@NeoSoft.com (Karl Lehenbauer) for the code example. ------------------------------ From: -IX- How, in Tcl, can I XXX: Subject: -Q9O- trap signals, and other more Unix specific functions? A9O. Extended Tcl offers many of these types of functions. For instance, extended Tcl has the 'signal' command: signal action siglist [command] where action is one of "default", "ignore", "error", "trap", "get", plus the POSIX "block" and "unblock" actions (available only on POSIX systems, of course). Siglist is a list of either the symbolic or numeric Unix signal (the SIG prefix is optional). Command is your error handler (or a simple {puts stdout "Don't press *that* key!"} :-) "trap" does what you expect, and I find "error" and "get" to be extremely useful in interactive programs which demand keyboard traversal. Extended Tcl also has things like fork, etc. Answer by brad@NeoSoft.com (Brad Morrison). ------------------------------ From: -X- How, in Tcl, can I XXX: Subject: -Q9P- get quoted strings to work the way I want? A long article dealing with the issues can be found at harbor.ecn.purdue.edu:/pub/tcl/docs/README.programmer Here are some short answers: Q. I'm trying to build up a command for later execution but am having trouble with variable values that include whitespace or special characters. A. The safest way to build up commands is to use the list command so that you can keep track of the list structure. Avoid using double quotes because you can end up with an extra trip through the evaluator. We'll illustrate this with a command to create a button that prints out the label on the button when you click it. Wrong answer #1: button $myname -text $label -command "puts stdout $label" Why? because if $label has whitespace then the puts command will be passed the wrong number of arguments. If $label has $ or [ ] characters, they will be interpreted instead of printed. Good answer #2: button $myname -text $label -command [list puts stdout $label] Why? because list will properly quote the value of $label Q. I'm trying to build up a command for later execution but am having trouble getting some variables to evaluate now, and some to evaluate later when the command is run. A. The cleanest way to do this is to define a procedure that hides the use of the variables at run time, and then build up a call to that procedure using the list command as described previously. (You can even define the procedure on the fly. It will have global scope even it if is created within another procedure.) Wrong answer #1: button $myname -text $label -command \ [list puts stdout $ArrayOfDynamicStuff($label)] Why? The array value will be substituted when the button is created, not later on when the button is clicked. Also, note that the command is executed at the global scope, so it is not necessary to include a "global ArrayOfDynamicStuff" in the command. Wrong answer #2 (backquotes and list): button $myname -text $label -command \ [list puts stdout \$ArrayOfDynamicStuff($label)] Why? Here the list command and the backquote of $ are fighting with each other. The command ends up being something like: puts stdout {$ArrayOfDynamicStuff(foo)} which prevents the substitution of the value of the array element. Dubious answer #3 (backquotes and double-quotes): button $myname -text $label -command \ "puts stdout \$ArrayOfDynamicStuff($label)" Why? This only works if the value of $label has no special characters or whitespace. Clean answer #4 (proc): proc doit { i } { global ArrayOfDynamicStuff puts stdout $ArrayOfDynamicStuff($i) } button $myname -text $label -command [list doit $label] Why? Using little TCL procs for your button commands is a good habit because it eliminates most needs for fancy quoting, and it makes it easier to tweak the button command later on. Q. I'm trying to pass along a variable number of args to another procedure but I'm having trouble getting the $args to expand right. A. Avoid using eval and double quotes because that results in an extra trip through the interpreter. The eval command will do a concat of its arguments if there are more than one, so that pretty much eliminates the need to group things with double quotes. Let's extend the button example: Wrong answer #1: proc mybutton { myname label args } { button $myname -text $label -command [list puts stdout $label] $args } Why? All the extra arguments to mybutton are grouped into one list element that is but into the value of $args. However, the button command expects to see individual arguments, not a sub-list. Wrong answer #2: proc mybutton { myname label args } { eval "button $myname -text $label -command [list puts stdout $label] $args" } Why? The double quotes allow expansion of $label as well as $args, so if $label has any whitespace, the button command will be malformed Good answer #3: proc mybutton { myname label args } { set cmd {button $myname -text $label -command [list puts stdout $label]} eval $cmd $args } Why? Eval will first concatenate its two arguments and then run the result through the interpreter. Think of this as stripping off the outer curly braces from $cmd and $arg and making a single list with all the elements of both. $label will be evaluated exactly once, so the puts command will remain good, and whatever went into args will also be processed exactly one time. Q. Why do I get a syntax error in an if/while/for statement? A. You may have written something like wish: set foo bar wish: if {$foo == bar} {puts stdout bar} syntax error in expression "$foo == bar" in which bar is interpreted as neither a string nor a variable, since strings as operands in expressions MUST be surrounded by double quotes or braces. Change to wish: if {$foo == "bar"} {puts stdout bar} or wish: if {$foo == {bar}} {puts stdout bar} always in expressions, depending on if you want expansion performed or not. Contributed by "Jesper Blommaskog" ------------------------------ From: -X- How, in Tcl, can I XXX: Subject: -Q9Q- share procedures between multiple tcl applications? A9Q. Rather than sourceing the files explicitly, build a tcl library: Step 1. Put the files in a common directory Step 2. Build the tclIndex for the "library". I use a Makefile with a convention like: install.index: (cd ${DESTDIR}/tclscripts/lib; \ echo 'source /usr/local/lib/tcl/init.tcl;\ auto_mkindex . *.tk' | tcl ; exit 0) Step 3. Modify your tcl scripts to reference the library: e.g.: # local additions lappend auto_path /usr/local/lib/tcl_local $env(RDS_TCL_SCRIPTS)/lib Now, as soon as your script tried to reference a procedure in the library, the "unknown" command autoloads the procedure for you. Contributed by Joe VanAndel ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.A.1- get my wish application to execute - I just get a wish prompt! Or I just get error msgs about permission denied, not found, etc. A10.A.1. Most systems require a full pathname to the interpreter. So you cannot start a wish script out as #! wish -f Likewise, many Unix systems have a maximum length of characters that you can put on a #! line. If you exceed this, you do not get the behaviour you expect. So do not try to put something like: #! /projects/somethingbig/bin/sun4/wish -f followed by your wish code. Keep the lines short - under 30 characters is recommended. ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.A.2- ,using a machine with less than 8 bit color, run? A10.A.2. Tk doesn't behave very well with less than 8-bit color screens. To try to use it, find all the places in the Tk/wish source where DefaultDepthOfScreen is invoked to test the number of bit-planes. Change all of these to pretend there is just 1 bit-plane, or call a procedure which monitors a Tcl variable so that it is configurable, and you should be okay. Another alternative is to see if the server you are using has alternative visual / color models, such as static visual, etc. One of the alternatives may allow Tk to work better. Thanks to "Nathaniel Borenstein" for this info! ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.A.3- set X11 resources for a wish application in an app-defaults file? A10.A.3. Read the documentation for the option command. Then you should consider something like the following - assume the program name is xwf. The following are two general purpose functions to put into a library: # envVal envValName # Looks up the envValName environment variable and returns its # value, or {} if it does not exists proc envVal {envValName} { global env if [info exists env($envValName)] {return $env($envValName)} {return {}} } # loadAppDefaults classNameList ?priority? # Searches for the app-default files corresponding to classNames in # the order specified by X Toolkit Intrinsics, and loads them with # the priority specified (default: startupFile). proc loadAppDefaults {classNameList {priority startupFile}} { set filepath "[split [envVal XUSERFILESEARCHPATH] :] \ [envVal XAPPLRESDIR] \ [split [envVal XFILESEARCHPATH] :] \ /usr/lib/X11" foreach i $classNameList { foreach j $filepath { if {[file exists $j/$i]} { option readfile $j/$i $priority; break } } } } # Now, here is what you would put into xwf: option add Tk.BoldFont "*-lucida sans-Bold-R-Normal-*-100-*" widgetDefault loadAppDefaults {xwf XWF} userDefault This sets a program default, then load any defaults specified in the user's default resources and finally any site or general app-defaults resource. Of course, you would want to add some xwf command line handling to allow the user to override things at execution time. ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.A.4- specify bitmap patterns on the command line instead of just as a file name? A10.A.4. You can not, at least as of June, 1992. ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.A.5- get a Motif user interface? A10.A.5. Tk does not currently use the Xt toolkit, so a strict adherence to Motif via the libXm.a routines is not possible. However, the authors of Tk prefer the Motif style of user interface, so you will find that Tk makes quite an attempt to implement a Motif-like interface. ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.A.6- get an OpenLook user interface? A10.A.6. Unfortunately, Tk does not currently use either XView or Xt based widgets in its user interface, so an OpenLook compliant (or even similar) interface is probably not easily achievable in the near future. ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.B.1- change the default class bindings? A10.B.1. All default class bindings for Tk widgets are initialized in $tk_library/tk.tcl. Use this file as a guide to implement new bindings. For instance, the following code duplicates Button 1's drag-select facility in Button 3 for all listboxes: bind Listbox <3> {%W select from [%W nearest %y]} bind Listbox {%W select to [%W nearest %y]} ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.B.2- delete a binding? A10.B.2. Give an empty-string command to the "bind" invocation. For example, to disable the Delete key in all entry fields: bind Entry {} ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.B.3- change a binding while it is being executed? A10.B.3. As of June, 1992, this was not a safe thing to do in Tk. It was put on the bug list by John Ousterhout to be fixed in a future version. The solution for now is not to change the bindings, but to change something in the code they execute. For example, keep a state variable that indicates which binding you'd like, but always have the binding call a given procedure. Then that procedure checks the variable and executes one piece of code or another. Or, you could just make the binding's command "eval $cmd" and then change the variable "cmd" depending on your application's state. ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.B.4- bind the arrow key on my Sun keyboard? A10.B.4. You have to call it rather than . Under X11, keys are referred to by their keysym. One can use either xmodmap -pk or the xev program to determine what the keysym a particular key on a keyboard is currently generating. If the keysym that is being used is not known by Tk, you may have to edit its ks_names.h file. There is a note in this file that indicates that one should not edit it - but this is where the keysym must be for it to be recognized. Thanks to Wayne Christopher for this note. ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.B.5- get root's mouse bindings to work in my Tk application? A10.B.5. Some window managers, such as mwm, define mouse button bindings which cause Tk some problems. Try saving off the window manager's startup file (something like /.mwmrc for instance) and then copy in a startup file from a login id that works. Thanks to brad@NeoSoft.com (Brad Morrison) for this invaluable tip! ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.C.1- get an application to also use libXt? A10.C.1. Tk2.1 and Xt have different X connections, and XtAppNextEvent will block is there is nothing coming from the X connection. One way of fixing this is get the connection number of Tk using ConnectionNumber(Tk_Display(tk_window)); and using XtAddInput to register this with the Xt event handler. The callback procedure for XtAddInput wrapper procedure that runs Tk_OneEvent(1). There might be problems with Tk file sources which aren't registered with Xt. Thanks to joe@astro.as.utexas.edu (Joe Wang) for this information. ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.C.2- change the X11 cursor? A10.C.2. Here is a tip from mgc@cray.com (M. G. Christenson). Look at /usr/include/X11/cursorfont.h for a list of available cursors. You can use the names in there by removing the 'XC_'. Here's a little proc I use to make my entire application go 'busy' while it's doing something. Just call it with the commands you want to execute, and the watch cursor will be displayed for the time it takes the commands to complete. Note that any new windows will have their normal cursor. proc busy {cmds} { global errorInfo set busy {.app .root} set list [winfo children .] while {$list != ""} { set next {} foreach w $list { set class [winfo class $w] set cursor [lindex [$w config -cursor] 4] if {[winfo toplevel $w] == $w || $cursor != ""} { lappend busy [list $w $cursor] } set next [concat $next [winfo children $w]] } set list $next } foreach w $busy { catch {[lindex $w 0] config -cursor watch} } update idletasks set error [catch {uplevel eval [list $cmds]} result] set ei $errorInfo foreach w $busy { catch {[lindex $w 0] config -cursor [lindex $w 1]} } if $error { error $result $ei } else { return $result } } ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.C.3- raise or lower a window? A10.C.3. This is on the (semi-infinite) list of things to be done in the future. If you have the time, please go ahead and add it and submit the code and all will be grateful. ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.C.4- re-map a withdrawn window id? A10.C.4. Use wm deiconify . ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.C.5- use Tk in a subwindow of a non-Tk X11 application? A10.C.5. From faustus@ygdrasil.CS.Berkeley.EDU (Wayne A. Christopher): [C]reate the Tk toplevel window but don't map it (wm withdraw). Then re-parent the window to be a subwindow of your other one and then map it. I have done this when the Tk application is a separate process, but if it's the same process I think you will get into trouble with the event loop, since each toolkit wants control. ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.D.1- resize a listbox? A10.D.1. Use wm min/maxsize - in a uniform manner. Here is a resizable listbox: #!/usr/local/bin/wish -f wm minsize . 20 20 wm maxsize . 1152 900 pack append . [listbox .l -borderwidth 2 -relief raised] {expand fill} Doing the same with the text widget brings its resizing under control too. Thanks to "John C Ellson" for this tip. ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.E.1- get output from a Tk canvas? A10.E.1. The latest Tk has a save suboption on canvas which allows one to create a file describing the canvas. The default output is Encapsulated Postscript, but there is an xpm3 suboption as well. ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.E.2- fill a canvas which is bounded by lines as opposed to a shape like a polygon, oval, etc.? A10.E.2. No, you have to at least use a polygon if you want to fill an area bounded by some lines. ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.E.3- raise/lower canvas window objects or draw graphics onto a window object inside a canvas? A10.E.3. You can't yet. "Jesper Blommaskog" . ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.F.1- get the name of my own interpreter? A10.F.1. gah@att.com (George A. Howlett) points us to the winfo manual page - winfo name . gets the name of the current application. ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.F.2- get -relief to work on my text widgets? A10.F.2. From Owen Rees , we find out that we must: "[m]ake the border width non-zero as in" text .t -width 20 -height 20 -relief sunken -borderwidth 4 ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.F.3- create a scrollable window of buttons? A10.F.3. There are at least two ways to do this. First, there is a hypertext widget that one can get from the Tcl User Contributed Code Archive (see comp.lang.tcl FAQ part 3 for details) which provides such a facility. And here is some sample code from "Michael Moore" which shows a way to do this using just Tk. #! /bin/wish -f # # This demonstrates how to create a scrollable canvas with mutliple # buttons. # # Author : Michael Moore # Date : November 17, 1992 # # # This procedure obtains all the items with the tag "active" # and prints out their ids. proc multi_action {} { set list [.frame.canvas find withtag "active"] puts stdout "Active Item Ids : " foreach item $list { puts stdout $item } } # # This simulates the toggling of a command button... # Note that it only works on a color display as is right now # but the principle is the same for b&w screens. # proc multi_activate {num id} { set tags [.frame.canvas gettags $id] if {[lsearch $tags "active"] != -1} { .frame.canvas dtag $id "active" .frame.canvas.button$num configure \ -background "#060" \ -activebackground "#080" } else { .frame.canvas addtag "active" withtag $id .frame.canvas.button$num configure \ -background "#600" \ -activebackground "#800" } } proc setup {} { frame .frame scrollbar .frame.scroll \ -command ".frame.canvas yview" \ -relief raised canvas .frame.canvas \ -yscroll ".frame.scroll set" \ -scrollregion {0 0 0 650} \ -relief raised \ -confine false \ -scrollincrement 25 pack append .frame \ .frame.scroll {left frame center filly} \ .frame.canvas {left frame center fillx filly} pack append .\ .frame {left frame center fillx filly} button .frame.canvas.action \ -relief raised \ -text "Action" \ -command "multi_action" .frame.canvas create window 1 25 \ -anchor w \ -window .frame.canvas.action for {set i 2} {$i < 26} {incr i} { button .frame.canvas.button$i \ -relief raised \ -background "#060" \ -foreground wheat \ -activebackground "#080" \ -activeforeground wheat \ -text "Button $i" set id [.frame.canvas create window 1 [expr $i*25] \ -anchor w \ -window .frame.canvas.button$i] .frame.canvas.button$i configure \ -command "multi_activate $i $id" } } setup ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.F.4- pack a text widget so that it can be resized interactively? A10.F.4. From Spencer W. Thomas we find that we need to: wm minsize . 0 0 text .text pack append . .text {fill expand} ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.F.5- create a widget with an upper case name? A10.F.5. During a recent revision of Tk, things were changed so that names beginning with a capital letter are reserved for class names. Specific instances of widgets must begin with a lower case letter. This enables X11 resource definitions to distinguish between a class and instance. ------------------------------ From: -X- How, in Tk, can I XXX: Subject: -Q10.F.6- create equal sized buttons? A10.F.6. Recently, Micael Salmon posted: In article <1993Jun23.065417.4302@ericsson.se>, I write: |> I am currently working on yet another tn3270 emulator and I have run |> into a problem with creating equal sized buttons. I have created an |> array of buttons for PF and cursor keys and I would now like to make |> them all the same size. Arranging them into columns was not |> a problem but when I add bitmaps the buttons lose their horizontal |> alignment. What I tried was to read the height and width of the buttons |> using winfo, determine the largest and then use pads to force them to |> be the same size, this doesn't seem to work. The technique of using |> pre-set height and width doesn't seem applicable when using a mixture |> of bitmaps and text as the size in pixels of a text button is font |> dependent. All suggestions welcome. Jim Wight suggested using reqheight and reqwidth and then specifying padx and pady in the pack command for each button. Jim says: I think it only fair to point out that it was Tuomas J Lukka who suggested the use of reqwidth and reqheight when I asked how to get over the deficiencies in my first attempt at a solution that I posted to this group. Since you're all agog I might as well post the final solution (more or less) that I mailed to Michael. frame .frame1 frame .frame2 button .frame1.a -text "pretty long button text" button .frame1.b -text "short one" button .frame2.c -bitmap "@/usr/include/X11/bitmaps/xlogo32" button .frame2.d -text "tiny" set long [winfo reqwidth .frame1.a] set short [winfo reqwidth .frame1.b] set medium [winfo reqwidth .frame2.c] set tiny [winfo reqwidth .frame2.d] set pady [expr [winfo reqheight .frame2.c]-[winfo reqheight .frame2.d]] pack append .frame1 .frame1.a "filly pady $pady" pack append .frame1 .frame1.b "fillx padx [expr $long-$short] filly pady $pady"pack append .frame2 .frame2.c "fillx padx [expr $long-$medium] filly" pack append .frame2 .frame2.d "fillx padx [expr $long-$tiny] filly pady $pady" pack append . .frame1 {left} .frame2 {left} ------------------------------ End of comp.lang.tcl Frequently Asked Questions (2/5) ***************************************************** -- :s :s Larry W. Virden INET: lvirden@cas.org :s Personal: 674 Falls Place, Reynoldsburg, OH 43068-1614